// Copyright (c) 2012-2014 The Bitcoin developers
// Copyright (c) 2014-2015 The Dash developers
// Copyright (c) 2015-2018 The Luxcore developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef ACTIVEMASTERNODE_H
#define ACTIVEMASTERNODE_H

#include "uint256.h"
#include "sync.h"
#include "net.h"
#include "key.h"
//#include "primitives/transaction.h"
//#include "main.h"
#include "init.h"
#include "wallet/wallet.h"
#include "darksend.h"


class CRPCTable;
class CWallet;
class JSONRPCRequest;
class UniValue;
struct PartiallySignedTransaction;
class CTransaction;

// Responsible for activating the masternode and pinging the network
    class CActiveMasternode {
    public:
        // Initialized by init.cpp
        // Keys for the main masternode
        CPubKey pubKeyMasternode;

        // Initialized while registering masternode
        CTxIn vin;
        CService service;

        int status;
        std::string notCapableReason;

        CActiveMasternode() {
            status = MASTERNODE_NOT_PROCESSED;
        }

        void ManageStatus(CConnman* connman); // manage status of main masternode

        bool Dseep(CConnman* connman, std::string &errorMessage); // ping for main masternode
        bool Dseep(CConnman* connman, CTxIn vin, CService service, CKey key, CPubKey pubKey, std::string &retErrorMessage,
                   bool stop); // ping for any masternode

        bool StopMasterNode(CConnman* connman, std::string &errorMessage); // stop main masternode
        bool StopMasterNode(CConnman *connman, std::string strService, std::string strColletralAddress, std::string strKeyMasternode,
                            std::string &errorMessage); // stop remote masternode
        bool StopMasterNode(CConnman* connman, CTxIn vin, CService service, CKey key, CPubKey pubKey,
                            std::string &errorMessage); // stop any masternode

        bool Register(CConnman *connman ,std::string strService, std::string strKey, std::string txHash, std::string strOutputIndex,
                      std::string &errorMessage); // register remote masternode
        bool Register(CConnman* connman ,CTxIn vin, CService service, CKey key, CPubKey pubKey, CKey keyMasternode,
                                             CPubKey pubKeyMasternode,
                                             std::string &retErrorMessage); // register any masternode
        bool RegisterByPubKey(CConnman *connman, std::string strService, std::string strKeyMasternode,
                              std::string collateralAddress,
                              std::string &errorMessage); // register for a specific collateral address

        // get 10000 XLT input that can be used for the masternode
        bool GetMasterNodeVin(CTxIn &vin, CPubKey &pubkey, CKey &secretKey);

        bool GetMasterNodeVin(CTxIn &vin, CPubKey &pubkey, CKey &secretKey, std::string strTxHash,
                              std::string strOutputIndex);

        bool GetMasterNodeVinForPubKey(std::string collateralAddress, CTxIn &vin, CPubKey &pubkey, CKey &secretKey);

        bool GetMasterNodeVinForPubKey(std::string collateralAddress, CTxIn &vin, CPubKey &pubkey, CKey &secretKey,
                                       std::string strTxHash, std::string strOutputIndex);

        vector <COutput> SelectCoinsMasternode();

        vector <COutput> SelectCoinsMasternodeForPubKey(std::string collateralAddress);

        bool GetVinFromOutput(COutput out, CTxIn &vin, CPubKey &pubkey, CKey &secretKey);

        // enable hot wallet mode (run a masternode with no funds)
        bool EnableHotColdMasterNode(CTxIn &vin, CService &addr);
    };
#endif
